<?php
/**
 * Profiles Model for Congregation Component
 * 
 * @package    Congregation
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * Sharing Sharing Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class CongregationModelProfiles extends CongregationModel
{

	var $_data;


  /**
   * Returns the query for listing the categories
   *
   * @access  protected
   * @return  string    The query to be used to retrieve the categories data from the database
   * @since   1.5.5
   */
  function _buildQuery()
  {
	
	
	$query = "SELECT m.*, t.t_charge, u.name, u.email, s.abbr FROM #__congregation_profile AS m " .
	" LEFT JOIN #__users AS u ON m.username=u.username " .
	" LEFT JOIN #__congregation_profile_paths AS t ON (t.t_username = m.username AND t.t_current= 1) " .
	" LEFT JOIN #__congregation_casa AS c ON t.t_cid=c.c_id " . 
	//" LEFT JOIN #__congregation_charge AS r ON m.charge=r.id " .
	" LEFT JOIN #__congregation_status AS s ON m.status=s.id " .
	$this->_buildWhere() .
	$this->_buildOrderby();
	
	

    return $query;
  }


	function _buildWhere()
	{
	
		$search = JRequest::getVar('search');
		$p_id	= JRequest::getVar('p_id'); 
		$c_id	= JRequest::getVar('c_id'); 
	
		$where = array();
		if($p_id) $where[] = ' p_id = ' . $p_id;
		if($c_id) $where[] = ' c.c_id = ' . $c_id;
	
		if ($search) {
			$where[] = '(LOWER(m.lname) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ) . ' OR ' .
						'LOWER(m.fname) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ) . ' OR ' .
						'LOWER(m.username) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ) . ' OR ' .
						'LOWER(u.email) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ) . ')';
		}
		
		$where = count($where) ? 'WHERE ' . implode(' AND ', $where) : '';
		return $where;
	}
	
	function _buildOrderby()
	{
		$order_by = JRequest::getVar('order_by');
		$order_dir = JRequest::getVar('order_dir');

		$ordering	= ' ORDER BY '. $order_by .' '. $order_dir;

		return $ordering;

	}
  
	/**
	* Method to get province list according to params
	*
	*/

	
	function getData()
	{
		// Lets load the data if it doesn't already exist
		if (empty( $this->_data ))
		{
			
			$query = $this->_buildQuery();
			$limit      = JRequest::getVar('limit', 0, '', 'int');
			$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
			
			if($limit > 0)
			{
			$query .= " LIMIT $limitstart, $limit ";
			}


			$this->_db->setQuery( $query );
			
			$this->_data = $this->_getList( $query );
			
		}
		return $this->_data;
	
	}
	
		/**
		* Method to get the total number of profiles
		*
		* @access  public
		* @return  int     The total number of categories
		* @since   1.5.5
		*/
		function getTotal()
		{
		// Let's load the number of categories if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			
			$this->_total = $this->_getListCount($query);
			
		}

		return $this->_total;
		}


}